﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;

namespace weiqijiance.控制流程
{
    public class socketserver
    {
        public mainform m_mainform;
        Socket sSocket;
        public int port = 7700;
        public string host = "127.0.0.1";
        public void server()
        {
            try
            {
                IPAddress ip = IPAddress.Parse(host);

                IPEndPoint ipe = new IPEndPoint(IPAddress.Any, port);

                sSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sSocket.Bind(ipe);
                sSocket.Listen(10);
                Console.WriteLine("监听已经打开，请等待");
                accept();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //receive message
        }
        public void accept()
        {
            try
            {
                serverSocket = sSocket.Accept();
                Console.WriteLine("连接已经建立");
                bbz = true;
            }
            catch 
            {
               
            }
        }
        Socket serverSocket;
        public string baotou = "";
        public string receive()
        {
            try
            {
                baotou = "";
                string recStr = "";
                byte[] recByte = new byte[4096];
                int bytes = serverSocket.Receive(recByte, recByte.Length, 0);

                var sdata = System.Text.Encoding.GetEncoding("GB2312").GetString(recByte, 0, bytes);
                if (sdata.Length > 0)
                {
                    if (sdata.Contains("&"))
                    {
                        string[] data = sdata.Split('&');
                        if (data.Length > 1)
                        {
                            baotou = data[0];
                            recStr += data[1];
                        }
                    }
                }

                
                var Rec_data = string.Format(@"{0}&{1}", baotou, "接收数据成功返回包头");

                send(Rec_data);
                System.Threading.Thread.Sleep(200);
                //recStr += System.Text.Encoding.GetEncoding("GB2312").GetString(recByte, 0, bytes);
                return recStr;
            }
            catch (Exception ex)
            {
                return "断开需要重连"+ex.ToString();
            }
        }
        public void send(string sendStr)
        {
            try
            {
              
                byte[] sendByte = System.Text.Encoding.GetEncoding("GB2312").GetBytes(sendStr);// Encoding.ASCII.GetBytes(sendStr);
                serverSocket.Send(sendByte, sendByte.Length, 0);
            }
            catch 
            {
            }
        }
        public void close()
        {
            serverSocket.Close();
            sSocket.Close();
        }
        bool bbz = false;
        public void threadopen()
        {
            //控制流程.flowcrtl.m_mainform = this.m_mainform;
            server();
            string sss = "";
            while (true)
            {
                System.Threading.Thread.Sleep(10);
                if (bbz)
                {
                    sss = receive();
                    m_mainform.baocunriji("接收到调度发送内容",sss);
                    if (sss.Contains("断开需要重连"))
                    {
                        sss = "";
                        accept();
                    }
                    else
                    {
                        dy(sss);
                      
                    }
                    sss = "";
                }
            }
        }
        public void dy(string sml)
        {
           
            string[] smls = sml.Split(',');
            if (smls.Length >= 4)
            {
                if (smls[0] == "S")
                {
                   
                    for (int i = 0; i < m_mainform.listflow.Count; i++)
                    {
                        if (smls[2].ToString().Trim() == m_mainform.listflow[i].sshebei.Trim())
                        {
                            m_mainform.baocunriji("接收333", string.Format("工位名{0}:{1}", smls[2].ToString().Trim(), m_mainform.listflow[i].izhuangtai));
                            if (m_mainform.listflow[i].izhuangtai == 1)
                            {
                                send(sml + "," + "工位忙碌中");
                            }
                            else
                            {
                                m_mainform.listflow[i].sDetectID = smls[1];
                                m_mainform.listflow[i].izhuangtai = 2;
                                m_mainform.baocunriji("接收到状态", string.Format("工位名{0}:{1}", smls[2].ToString().Trim(), m_mainform.listflow[i].izhuangtai));
                                //send(sml + "," + "成功");
                            }
                        }
                    }
                }
                else if (smls[0] == "E")
                {
                    for (int i = 0; i < m_mainform.listflow.Count; i++)
                    {
                        if (smls[2].ToString().Trim() == m_mainform.listflow[i].sshebei.Trim())
                        {
                            if (m_mainform.listflow[i].sDetectID == smls[1])
                            {
                                if (m_mainform.listflow[i].izhuangtai == 1)
                                {
                                    m_mainform.listflow[i].izhuangtai = 3;
                                    //send(sml + "," + "成功");
                                }
                                else
                                {

                                }
                            }
                        }
                    }
                }
            }
            else
            {
                send(sml + "," + "成功");

            }

        }
    }
}
